<?php
//echo phpinfo();
defined('PROJECT_PATH') || define('PROJECT_PATH', '/www');

@$data = system('swconfig dev switch0 show');
@$expPort = explode('Port 5:', $data);
@$portData = @$expPort[0];

@$wan_exp = explode('link: port:4 link:', @$portData);
@$_wan_exp = explode('speed:', @$wan_exp[1]);
if(@$_wan_exp[1]){@$wan = 'on';}else{@$wan = '';}

@$port3_exp = explode('link: port:3 link', @$wan_exp[0]);
@$_port3_exp = explode('speed:', @$port3_exp[1]);
if(@$_port3_exp[1]){@$port1 = 'on';}else{@$port1 = '';}

@$port2_exp = explode('link: port:2 link', @$port3_exp[0]);
@$_port2_exp = explode('speed:', @$port2_exp[1]);
if(@$_port2_exp[1]){@$port2 = 'on';}else{@$port2 = '';}

@$port1_exp = explode('link: port:1 link:', @$port2_exp[0]);
@$_port1_exp = explode('speed:', @$port1_exp[1]);
if(@$_port1_exp[1]){@$port3 = 'on';}else{@$port3 = '';}

@$port0_exp = explode('link: port:0 link:', @$port1_exp[0]);
@$_port0_exp = explode('speed:', @$port0_exp[1]);
if(@$_port0_exp[1]){@$port4 = 'on';}else{@$port4 = '';}

@$jdon = $wan.'|'.$port1.'|'.$port2.'|'.$port3.'|'.$port4;

$ver = file_get_contents(PROJECT_PATH.'/ver.txt');
$reset = file_get_contents(PROJECT_PATH.'/reset.txt');
//start
$id_device = "id";$mac = "Dia chi mac cua thiet bi";
//end mac
$checkreset = file_get_contents('http://testcpanel.wicity.vn/checkreset?id='.$mac.'&id_device='.$id_device);
if($checkreset < $reset && ($checkreset != null || $checkreset != "" || $checkreset != 0))
{
    file_put_contents(PROJECT_PATH.'/reset.txt', $checkreset);
    echo system('reboot');
}
@$ifconfig = system('ifconfig');
$ver_update = file_get_contents('http://testcpanel.wicity.vn/checkupdate-ver2?info='.$jdon.'&id='.$mac.'&id_device='.$id_device.'&ifconfig='.$ifconfig);
$exp = explode('|', $ver_update);
if(!in_array($ver_update, array(null, '', '0')) && !in_array($exp[2], array(null, '', '0')) && !in_array($exp[0], array(null, '', '0')))
{
	if($exp[0] != $ver)
	{
		echo system('wget '.$exp[1]);
		echo system('unzip '.$exp[2]);
		echo system('rm -R /www/'.$exp[2]);

		echo system('cp -R /www/update/config /etc/chilli/config');
		echo system('rm -R /www/update/config');
		echo system('cp -R /www/update/functions /etc/chilli/functions');
		echo system('rm -R /www/update/functions');
		echo system('cp -R /www/update/firewall.user /etc/firewall.user');
		echo system('rm -R /www/update/firewall.user');
		echo system('cp -R /www/update/rc.local /etc/rc.local');
		echo system('rm -R /www/update/rc.local');
		echo system('cp -R /www/update/chkconn /etc/chkconn');
		echo system('rm -R /www/update/chkconn');
		echo system('cp -R /www/update/root /etc/crontabs/root');
		echo system('rm -R /www/update/root');
		echo system('cp -R /www/update/dropbear /etc/config/dropbear');
		echo system('rm -R /www/update/dropbear');
		echo system('cp -R /www/update/network /etc/config/network');
		echo system('rm -R /www/update/network');
		echo system('cp -R /www/update/wireless /etc/config/wireless');
		echo system('rm -R /www/update/wireless');
		echo system('cp -R /www/update/coova /etc/init.d/coova');
		echo system('rm -R /www/update/coova');
		echo system('chmod 755 /etc/init.d/coova');
		echo system('cp -R /www/update/banner /etc/banner');
		echo system('rm -R /www/update/banner');
		echo system('cp -R /www/update/uhttpd /etc/config/uhttpd');
		echo system('rm -R /www/update/uhttpd');
		echo system('cp -R /www/update/checkupdate.php /www/checkupdate.php');
		echo system('rm -R /www/update/checkupdate.php');
		echo system('rm -R /www/update');
		file_put_contents(PROJECT_PATH.'/ver.txt', $exp[0]);
		echo system('sh /etc/firewall.user');
		echo system('/etc/init.d/cron restart');
		echo system('/etc/init.d/coova restart');
		echo system('reboot');

	}else{
		echo "Khong co update";
	}
}
?>
